package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static cn.wolfcode.trip.base.domain.User.GENDER_NONE;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper mapper;
    @Override
    public User register(User user) {
        if (mapper.selectByEmail(user.getEmail()) != null){
            throw  new RuntimeException("邮箱已存在");
        }
        user.setPlace("中国");
        user.setGender(User.GENDER_NONE);
        user.setHeadImgUrl("/upload/head.jpg");
        user.setCoverImgUrl("/upload/bg.jpeg");
        user.setSign("个性签名");
        mapper.insert(user);
        return user;
    }

    @Override
    public User login(User user) {
     User info = mapper.selectByEmailAndPassword(user.getEmail(),user.getPassword());
        if (info == null) {
            throw new RuntimeException("邮箱和密码不匹配");
        }
        return info;
    }

    @Override
    public PageInfo<User> selectFroList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<User> users = mapper.selectAll(qo);
        return new PageInfo<User>(users);

    }

    @Override
    public void updateInfo(User user) {
        mapper.updateByPrimaryKey(user);
    }

    @Override
    public void updatePoint(Long id, Integer consume) {
        mapper.updatePoint(id,consume);
    }
}
